<?php
include_once ("video.php");
include_once ("uploadSlideManagement.php");
include_once ("page.php");

//*********************Video upload**********************

$errorVideo = 0;
$nameinfo = pathinfo($_FILES['videoUpload1']['name']);
$dirname = $nameinfo['filename'] . '/';
//create a dir name based on the name of the video but without spaces
$dirname = str_replace(' ', '_', $dirname);
$fullpath = $GLOBALS['baseDir'] . "videos/$dirname";
if (file_exists($fullpath)) {
	header("location:index.php?page=upload&errV=-5");
	exit ;
}
$old = umask(0);
$success = mkdir($fullpath, 0755, TRUE);
umask($old);
if (!$success) {
	//no folder created
	header("location:index.php?page=upload&errV=-4");
	exit ;
}
for ($i = 1; $i <= 2; $i++) {
	$videoUpload = 'videoUpload' . $i;
	if ($_FILES[$videoUpload]['error'] > 0) {
		//stop and restart with the error shown
		header("location:index.php?page=upload&errV=" . $_FILES[$videoUpload]['error']);
		exit ;
	} else {
		$nameinfo = pathinfo($_FILES[$videoUpload]['name']);
		//move the file to the new directory
		$file = $nameinfo['filename'] . '.' . $nameinfo['extension'];
		$result = move_uploaded_file($_FILES[$videoUpload]['tmp_name'], $fullpath . $file);
		chmod($fullpath . $file, 0755);
		//if it is moved, add the video to the database and convert it.
		$extlessName = $nameinfo['filename'];
		$escExtlessName = str_replace(' ', '\ ', $extlessName);
		/*//convert the video
		 $movie = new ffmpeg_movie($fullpath . $tmpfile, FALSE);
		 $pid1;
		 $pid2;

		 //if there is a configuration file, do conversion in background
		 if (!($_FILES['slidesUploadFile']['size'] == 0 && $_FILES['slidesUploadFile']['error'] == 4)) {
		 exec("sudo /usr/bin/nohup avconv -i " . $fullpath . $tmpfile . " -threads 2 -s " . $movie -> getFrameWidth() . "x" . $movie -> getFrameHeight() . " -c:v libx264  -c:a ibvo_aacenc -ab 192k -crf 20 -y $fullpath$escExtlessName.mp4 > /dev/null 2>&1 &");
		 exec("sudo /usr/bin/nohup avconv -i " . $fullpath . $tmpfile . " -threads 2 -s " . $movie -> getFrameWidth() . "x" . $movie -> getFrameHeight() . " -vcodec libvpx -acodec libvorbis -aq 5 -ac 2 -qmax 25 -y $fullpath$escExtlessName.webm > /dev/null 2>&1 &");
		 } else {
		 //wait for the conversion to finish to be able to play the video in the slide management
		 exec("sudo /usr/bin/nohup avconv -i " . $fullpath . $tmpfile . " -threads 2 -s " . $movie -> getFrameWidth() . "x" . $movie -> getFrameHeight() . "  -c:v libx264  -c:a ibvo_aacenc -ab 192k -crf 20 -y  $fullpath$escExtlessName.mp4");
		 exec("sudo /usr/bin/nohup avconv -i " . $fullpath . $tmpfile . " -threads 2 -s " . $movie -> getFrameWidth() . "x" . $movie -> getFrameHeight() . " -vcodec libvpx -acodec libvorbis -aq 5 -ac 2 -qmax 25 -y $fullpath$escExtlessName.webm");
		 //delete the temporary uploaded file
		 unlink($fullpath . $tmpfile);
		 }*/
	}
}
//add to the database
$newVideo = new video();
if (isset($_POST['idCourse'])) {
	$dbErr = $newVideo -> addVideo($extlessName, $extlessName, "videos/" . $dirname, $_POST['idCourse']);
	if (!$dbErr) {
		//database error
		header("location:index.php?page=upload&errV=-2");
		exit ;
	}
} else {
	header("location:index.php?page=upload&errV=-2");
	exit ;
}

//***************Slides upload********************
$errorSlides = 0;
if ($_FILES['slidesUpload']['error'] > 0) {
	//save the error
	header("location:index.php?page=upload&errS=" . urlencode($_FILES['slidesUpload']['error']));
	exit ;
} else {
	$nameinfo = pathinfo($_FILES['slidesUpload']['name']);
	$fullpath = $GLOBALS['baseDir'] . "slides/$dirname";
	$old = umask(0);
	$success = mkdir($fullpath, 0755, TRUE);
	umask($old);
	if (!$success) {
		header("location:index.php?page=upload&errS=-2");
		exit ;
	}
	//move the file to the new directory
	$result = move_uploaded_file($_FILES['slidesUpload']['tmp_name'], $fullpath . $_FILES['slidesUpload']['name']);
	//if it is moved, add convert the pdf to several images.
	if ($result) {
		$extlessName = $nameinfo['filename'];
		//convert the video
		exec("sudo /usr/bin/nohup nice -n -19 convert $fullpath" . $_FILES['slidesUpload']['name'] . " $fullpath" . $extlessName . ".jpg");
		//delete the uploaded pdf file
		unlink("$fullpath" . $_FILES['slidesUpload']['name']);
	} else {
		//upload error
		header("location:index.php?page=upload&errS=-1");
		exit ;
	}

	//if everything goes well, show to the slides management page or process the configuration file
	$slideManagement = new uploadSlideManagement();
	$relativePath = str_replace($GLOBALS['baseDir'], '', $fullpath);
	//if there is no configuration file, load the page for manual configuration
	if ($_FILES['slidesUploadFile']['size'] == 0 && $_FILES['slidesUploadFile']['error'] == 4) {

		$page = new page();
		$page -> printHeaderHTMLAddSlides();
		$slideManagement -> displayVideo($newVideo);
		$slideManagement -> displaySlidesScrollBar($fullpath);
		$slideManagement -> displaySideListSlides($newVideo, $relativePath);

	} else {//if there is a configuration file
		$baseSlide = $relativePath . $extlessName;
		$slideManagement -> processConfigurationFile($_FILES['slidesUploadFile']['tmp_name'], $baseSlide, $newVideo -> getIdVideo());
		unlink($_FILES['slidesUploadFile']['tmp_name']);
		header("Location: index.php?page=videoList");
		exit ;
	}
}
?>